using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TheBestGameInTheWorld
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Texture2D spaceshipTexture;
        public Texture2D bulletTexture;
        public float posX = 100;
        public float posY = 100;
        public float rotation = 0;
        public bool shoot = false;
        public short shot = 0;


        public List<Projectile> projectiles = new List<Projectile>();


        private Vector2 origin;
        private Vector2 originBullet;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spaceshipTexture = Content.Load<Texture2D>("fighter");
            bulletTexture = Content.Load<Texture2D>("silver-bullet");
            origin.X = spaceshipTexture.Width / 2;
            origin.Y = spaceshipTexture.Height / 2;
            originBullet.X = bulletTexture.Width / 2;
            originBullet.Y = bulletTexture.Height / 2;
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (keyboard.IsKeyDown(Keys.Left)) rotation += -0.05f;
            if (keyboard.IsKeyDown(Keys.Right)) rotation += 0.05f;
            if (keyboard.IsKeyDown(Keys.Up))
            {
                posY += (float)Math.Sin((double)rotation + Math.PI /2) * 10;
                posX += (float)Math.Cos((double)rotation + Math.PI / 2) * 10;
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                posY -= (float)Math.Sin((double)rotation + Math.PI / 2);
                posX -= (float)Math.Cos((double)rotation + Math.PI / 2);
            }
            if (keyboard.IsKeyDown(Keys.Space) && shoot == false)
            {
                if (shot == 0)
                {
                    projectiles.Add(new Projectile(posX + (float)Math.Cos((double)rotation) * 30, posY + (float)Math.Sin((double)rotation) * 30, (float)(rotation + Math.PI / 2)));
                    shot = 1;
                }
                else
                {
                    projectiles.Add(new Projectile(posX - (float)Math.Cos((double)rotation) * 30, posY - (float)Math.Sin((double)rotation) * 30, (float)(rotation + Math.PI / 2)));
                    shot = 0;
                }
                shoot = true;
            }
            if (!keyboard.IsKeyDown(Keys.Space))
            {
                shoot = false;
            }

            int i = 0;

            while (i < projectiles.Count)
            {
                projectiles[i].posY += (float)Math.Sin((double)projectiles[i].angle) * 20;
                projectiles[i].posX += (float)Math.Cos((double)projectiles[i].angle) * 20;
                if (projectiles[i].posX < 0 || projectiles[i].posX > 2000 || projectiles[i].posY < 0 || projectiles[i].posY > 2000)
                {
                    projectiles.RemoveAt(i);
                }
                i++;
            }

            rotation = rotation % (float)(2 * Math.PI);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(spaceshipTexture, new Vector2(posX, posY), null, Color.White, rotation, origin, 1.0f, SpriteEffects.None, 0f);
            int i = 0;
            while (i < projectiles.Count)
            {
                spriteBatch.Draw(bulletTexture, new Vector2(projectiles[i].posX, projectiles[i].posY), null, Color.White, projectiles[i].angle, originBullet, 0.1f, SpriteEffects.None, 0f);
                i++;
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void Reset()
        {

        }
    }
}
